<?php
namespace Modules\Traiter;

use OSS\Core\OssException;
use OSS\OssClient;
use Src\Config;

trait Alioss
{
    protected static $accessKeyId = 'XXX';
    protected static $accessKeySecret = 'XXX';
    // Endpoint以杭州为例，其它Region请按实际情况填写。
    protected static $endpoint = "http://oss-cn-shenzhen.aliyuncs.com";//这里填写oss的内网地址，因为先传服务器再通过内网传oss
    // 存储空间名称
    protected static $bucket= "XXX";//物业小程序的专用bucket
    protected static $url = "https://images.xxx.com/";//物业协会的cdn加速域名，直接加文件名既可访问oss上的资源

    /**
     * @param mixed $object oss的文件名
     * @param mixed $filePath   文件路径
     * @return int 两时间的时间戳
     * @author             bobshipwood
     */
    protected static function oss_Upload($object,$filePath)
    {
        // 上传到oss的文件名称http://venucia2019.oss-cn-shanghai.aliyuncs.com/｛object｝
        //$object = '';
        //用putObject上传
        //$content = "Hello OSS";
        //用upload上传
        // <yourLocalFile>由本地文件路径加文件名包括后缀组成，例如/users/local/myfile.txt
        //$filePath = '';
        try{
            $ossClient = new OssClient(self::$accessKeyId, self::$accessKeySecret, self::$endpoint);
            //$ossClient->putObject($bucket, $object, $content);
            $ossClient->uploadFile(self::$bucket, $object, $filePath);
            return true;
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return false;
        }
    }

    /**
     * @return
     * @author             bobshipwood
     */
    public static function image_Upload()
    {
        //定义图片上传的格式
        $array = ['image/png','image/jpeg','image/jpg','image/gif'];
        if ($_FILES['image']['error'] > 0) {
            return false;
        } else {
            if ($_FILES['image']['size'] > 1024 * 1024 * 5) {
                return false;
            }
            if (!in_array($_FILES['image']['type'], $array)) {
                return false;
            } elseif ($_FILES['image']['type']=='image/png') {
                $ext = '.png';
            } elseif ($_FILES['image']['type']=='image/jpeg') {
                $ext = '.jpg';
            } elseif ($_FILES['image']['type']=='image/jpg') {
                $ext = '.jpg';
            } elseif ($_FILES['image']['type']=='image/gif') {
                $ext = '.gif';
            }
            $microtime = microtime(true);
            $mtime = explode('.',$microtime);
            $object = $mtime[0].$mtime[1].random(5);
            $objectname = $object.$ext;
            $res = self::oss_Upload($objectname,$_FILES['image']['tmp_name']);
            $fullurl = self::$url.$objectname;
            if ($res) {
                return $fullurl;
            } else {
                return false;
            }
        }
    }

    /** 多文件上传
     * @return array
     */
    public static function imagesUP()
    {
        $i = 0;
        $files = [];//仿造$_FILES所设的数组
        foreach ($_FILES as $file) {
            //key = 0 1 2   val = aaa.txt sadsad.mp4 sad.png
            foreach($file['name'] as $key => $val){
                //加入image是因为images_Upload()的时候写死了image
                $files[$i]['image']['name'] = $file['name'][$i];
                $files[$i]['image']['type'] = $file['type'][$i];
                $files[$i]['image']['tmp_name'] = $file['tmp_name'][$i];
                $files[$i]['image']['error'] = $file['error'][$i];
                $files[$i]['image']['size'] = $file['size'][$i];
                $i++;
            }
        }
        return $files;
    }

    /**多文件上传
     * @return false|string
     */
    public static function images_Upload($files)
    {
        //定义图片上传的格式
        $array = ['image/png','image/jpeg','image/jpg','image/gif'];
        if ($files['image']['error'] > 0) {
            return false;
        } else {
            if ($files['image']['size'] > 1024 * 1024 * 2) {
                return false;
            }
            if (!in_array($files['image']['type'], $array)) {
                return false;
            } elseif ($files['image']['type']=='image/png') {
                $ext = '.png';
            } elseif ($files['image']['type']=='image/jpeg') {
                $ext = '.jpg';
            } elseif ($files['image']['type']=='image/jpg') {
                $ext = '.jpg';
            } elseif ($_FILES['image']['type']=='image/gif') {
                $ext = '.gif';
            }
            $microtime = microtime(true);
            $mtime = explode('.',$microtime);
            $object = $mtime[0].$mtime[1].random(5);
            $objectname = $object.$ext;
            $res = self::oss_Upload($objectname,$files['image']['tmp_name']);
            $fullurl = self::$url.$objectname;
            if ($res) {
                return $fullurl;
            } else {
                return false;
            }
        }
    }

    //删除图片功能
    public static function oss_Del($object)
    {
        try{
            $ossClient = new OssClient(self::$accessKeyId, self::$accessKeySecret, self::$endpoint);
            $ossClient->deleteObject(self::$bucket, $object);
        } catch(OssException $e) {
            printf(__FUNCTION__ . ": FAILED\n");
            printf($e->getMessage() . "\n");
            return;
        }
    }
}
?>
